﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace BYManageInfoSystem
{
    public partial class Frm_NotLogisticsTruePayReg : Form
    {
        private DGBY.BLL.BYDepartTab m_bllBYDepartTab = new DGBY.BLL.BYDepartTab();
        private DGBY.BLL.FactoryDataTab m_bllFactoryDataTab = new DGBY.BLL.FactoryDataTab();
        private DGBY.BLL.BiZhiTab m_bllBiZhiTab = new DGBY.BLL.BiZhiTab();
        private DGBY.BLL.BYCompanyTab m_bllBYCompanyTab = new DGBY.BLL.BYCompanyTab();

        private DGBY.BLL.DZCompDataTab m_bllDZCompDataTab = new DGBY.BLL.DZCompDataTab();
        private DGBY.BLL.HKShipCompDataTab m_bllHKShipCompDataTab = new DGBY.BLL.HKShipCompDataTab();
        private DGBY.BLL.HKTransCompDataTab m_bllHKTransCompDataTab = new DGBY.BLL.HKTransCompDataTab();
        private DGBY.BLL.DHLDataTab m_bllDHLDataTab = new DGBY.BLL.DHLDataTab();
        private DGBY.BLL.BGHDataTab m_bllBGHDataTab = new DGBY.BLL.BGHDataTab();

        private DGBY.BLL.NotLogisticsTruePayRegTab m_bllNotLogisticsTruePayRegTab = new DGBY.BLL.NotLogisticsTruePayRegTab();

        private int m_nSelectNotLogisticsTruePayRegTabID = 0;
        private Dictionary<string, string> dicQueryCondition = new Dictionary<string, string>();

        string m_strQueryWhere = "";

        public Frm_NotLogisticsTruePayReg()
        {
            InitializeComponent();
        }
        public void LocalPrintPreveiw()
        {
            Infragistics.Shared.ResourceCustomizer rc = Infragistics.Win.Printing.Resources.Customizer;//Resources.Customizer;
            rc.SetCustomizedString("PrintPreview_DialogCaption", "打印预览");
            rc.SetCustomizedString("PrintPreview_Tool_Print", "打印(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_ClosePreview", "关闭(&C)");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuPreviewZoom", "显示比例");
            rc.SetCustomizedString("PrintPreview_Tool_Current_Page", "当前页");
            rc.SetCustomizedString("PrintPreview_Tool_Exit", "退出(&X)");
            rc.SetCustomizedString("PrintPreview_Tool_First_Page", "第一页");
            rc.SetCustomizedString("PrintPreview_Tool_Go_To", "跳至");
            rc.SetCustomizedString("PrintPreview_Tool_Last_Page", "最后一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_Page", "下一页");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_Page", "前一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_View", "下一视图(&N)");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_View", "前一视图(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_Hand_Tool", "手型工具(&H)");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Setup", "页面设置(&U)");
            rc.SetCustomizedString("PrintPreview_Tool_Snapshot_Tool", "快照工具(&S)");
            rc.SetCustomizedString("PrintPreview_Tool_View", "视图(&V)");
            rc.SetCustomizedString("PrintPreview_Tool_Whole_Page", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom", "缩放(&Z)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In", "放大");

            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out", "缩小");

            rc.SetCustomizedString("PrintPreview_ToolCategory_Context_Menus", "上下文菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_File", "文件");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Menus", "菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Tools", "工具栏");
            rc.SetCustomizedString("PrintPreview_ToolCategory_View", "视图");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Zoom_Mode", "缩放模式");

            rc.SetCustomizedString("PrintPreview_ToolTip_ClosePreview", "关闭");
            rc.SetCustomizedString("PrintPreview_ToolTip_Zoom", "缩放");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "页:{0}/{1}");

            rc.SetCustomizedString("CustomizeImg_ToolBar_MenuBar", "菜单");
            rc.SetCustomizedString("CustomizeImg_ToolBar_Standard", "标准");
            rc.SetCustomizedString("CustomizeImg_ToolBar_View", "视图");
            rc.SetCustomizedString("PrintPreview_Tool_File", "文件(&F)");
            rc.SetCustomizedString("PrintPreview_Tool_Tools", "工具(&T)");
            rc.SetCustomizedString("PrintPreview_Tool_Dynamic_Zoom_Tool", "动态缩放工具(&D)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out_Tool", "缩小工具");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In_Tool", "放大工具");
            // rc.SetCustomizedString("PrintPreview_Tool_Page_Layout","菜单");
            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PreviewRowColSelection_SelectedPages", "{0} x {1} 页");

            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Width", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_MarginWidth", "文字宽度");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_PageWidth", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_WholePage", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Layout", "页面布局");
            rc.SetCustomizedString("PrintPreview_Tool_Margin_Width", "文字宽度");
            rc.SetCustomizedString("ContextMenuPreviewHand", "缩放视图");


            rc.SetCustomizedString("PrintPreview_Tool_Reduce_Page_Thumbnails", "缩小");
            rc.SetCustomizedString("PrintPreview_Tool_Show_Page_Numbers", "显示页号");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuThumbnail", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Enlarge_Page_Thumbnails", "放大");
            rc.SetCustomizedString("PrintPreview_Tool_Thumbnails", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Continuous", "连续排序");
            //说明
            rc.SetCustomizedString("StatusBar_DynamicZoom_Instructions", "单击并拖动进行缩放操作");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "当前页: {0} / {1}");
            rc.SetCustomizedString("StatusBar_SnapShot_Instructions", "单击并拖动,系统将选定矩型区域复制到剪帖板");
            rc.SetCustomizedString("StatusBar_ZoomIn_Instructions", "单击并拖动,系统将放大选定矩型区域");
            rc.SetCustomizedString("StatusBar_ZoomOut_Instructions", "单击并拖动,系统将缩小选定矩型区域");
            rc.SetCustomizedString("StatusBar_Hand_Instructions", "单击并拖动以便显示更多内容");
        }

        private void InitCombox()
        {
            DataSet ds = m_bllFactoryDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxFac.DataSource = ds.Tables[0];

                this.comboBoxFac.ValueMember = "FactoryDataTabID";
                this.comboBoxFac.DisplayMember = "工厂名称";
                this.comboBoxFac.SelectedIndex = 0;
            }

            ds = m_bllBiZhiTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxMoneyType.DataSource = ds.Tables[0];
                this.comboBoxMoneyType.DisplayMember = "币制名称";
                this.comboBoxMoneyType.ValueMember = "BiZhiTabID";
                this.comboBoxMoneyType.SelectedIndex = 0;
            }

            ds = m_bllBYCompanyTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxComp.DataSource = ds.Tables[0];
                this.comboBoxComp.DisplayMember = "经营单位名称";
                this.comboBoxComp.ValueMember = "BYCompanyTabID";
                this.comboBoxComp.SelectedIndex = 0;
            }
            ds = m_bllBYDepartTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxDep.DataSource = ds.Tables[0];
                this.comboBoxDep.DisplayMember = "部门名称";
                this.comboBoxDep.ValueMember = "BYDepartTabID";
                this.comboBoxDep.SelectedIndex = 0;
            }
            ds = m_bllDZCompDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for(int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    this.comboBoxProvider.Items.Add(ds.Tables[0].Rows[i]["单证公司名称"].ToString());
                }

            }

            ds = m_bllHKShipCompDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    this.comboBoxProvider.Items.Add(ds.Tables[0].Rows[i]["香港船务公司名称"].ToString());
                }

            }

            ds = m_bllHKTransCompDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    this.comboBoxProvider.Items.Add(ds.Tables[0].Rows[i]["香港运输公司名称"].ToString());
                }

            }

            ds = m_bllDHLDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    this.comboBoxProvider.Items.Add(ds.Tables[0].Rows[i]["快递公司名称"].ToString());
                }

            }

            ds = m_bllBGHDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    this.comboBoxProvider.Items.Add(ds.Tables[0].Rows[i]["报关行名称"].ToString());
                }

            }
            
            this.comboBoxProvider.SelectedIndex = 0;
            
        }

        private void Frm_NotLogisticsTruePayReg_Load(object sender, EventArgs e)
        {
            LocalPrintPreveiw();
            InitCombox();
            InitQueryCondition();
        }

        private string MakeBianHao(string strDepName)
        {
            string strTemp = strDepName.Substring(0, 1);
            string strDate = System.DateTime.Now.Year.ToString();
            string strFind = strTemp + strDate;

            DataSet ds = m_bllNotLogisticsTruePayRegTab.GetAllNotLogisticsTruePayRegList("BYDepartTab.DepartName = '" + strDepName + "'"
                                                                                         + " and BianHao like " + "'%" + strFind + "%'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                string strDJNo = ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1]["编号"].ToString();
                string strNewBianHao = strDJNo.Substring(strDJNo.Length - 4, 4);
                int nNo = Convert.ToInt32(strNewBianHao, 10);
                nNo++;
                return strFind + nNo.ToString().PadLeft(4, '0');
            }
            else
            {
                return strFind + "0001";
            }
            

            return "";
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxTruePay))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxForeignJinE))
                    return;
                if (ClassInputValid.IsNum(this.errorProvider1, this.textBoxMonth))
                    return;

                DGBY.Model.NotLogisticsTruePayRegTab modelNotLogisticsTruePayRegTab = new DGBY.Model.NotLogisticsTruePayRegTab();

                modelNotLogisticsTruePayRegTab.WhetherGet = this.textBoxWhetherGet.Text;


                modelNotLogisticsTruePayRegTab.BianHao = MakeBianHao(this.comboBoxDep.Text);
                modelNotLogisticsTruePayRegTab.HuiKuanDate = this.dateTimePickerMoneyComeDate.Value;
                modelNotLogisticsTruePayRegTab.PayMonth = Convert.ToInt32(this.textBoxMonth.Text);
                modelNotLogisticsTruePayRegTab.ContractNo = this.textBoxContractNo.Text;
                modelNotLogisticsTruePayRegTab.TruePayMoney = Convert.ToDecimal(this.textBoxTruePay.Text);

                modelNotLogisticsTruePayRegTab.TruePayItem = this.textBoxTruePayItem.Text;
                modelNotLogisticsTruePayRegTab.AgentPerson = this.textBoxAgentPerson.Text;
                modelNotLogisticsTruePayRegTab.Provider = this.comboBoxProvider.Text;
                modelNotLogisticsTruePayRegTab.PublicFee = this.textBoxPublicFee.Text;
                modelNotLogisticsTruePayRegTab.PrivateFee = this.textBoxPrivateFee.Text;
                modelNotLogisticsTruePayRegTab.ForeignMoneyJinE = Convert.ToDecimal(this.textBoxForeignJinE.Text);

                if (this.comboBoxFac.SelectedValue != null)
                    modelNotLogisticsTruePayRegTab.FactoryDataTabID = Convert.ToInt32(this.comboBoxFac.SelectedValue.ToString(), 10);

                if (this.comboBoxMoneyType.SelectedValue != null)
                    modelNotLogisticsTruePayRegTab.BiZhiTabID = Convert.ToInt32(this.comboBoxMoneyType.SelectedValue.ToString(), 10);

                if (this.comboBoxDep.SelectedValue != null)
                    modelNotLogisticsTruePayRegTab.BYDepartTabID = Convert.ToInt32(this.comboBoxDep.SelectedValue.ToString(), 10);

                if (this.comboBoxComp.SelectedValue != null)
                    modelNotLogisticsTruePayRegTab.BYCompanyTabID = Convert.ToInt32(this.comboBoxComp.SelectedValue.ToString(), 10);




                m_bllNotLogisticsTruePayRegTab.Add(modelNotLogisticsTruePayRegTab);
                ShowAllRecordInDataGridView("");
                //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }
        private void ShowAllRecordInDataGridView(string strWhere)
        {
            ultraGrid1.DataSource = null;
            DataSet ds = m_bllNotLogisticsTruePayRegTab.GetAllNotLogisticsTruePayRegList(strWhere);

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            }
        }
        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView("");
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            string strNotLogisticsTruePayRegTabID = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells[0].Text;
            m_nSelectNotLogisticsTruePayRegTabID = Convert.ToInt32(strNotLogisticsTruePayRegTabID, 10);
            cellNotLogisticsTruePayRegTabClick();
        }

        private void cellNotLogisticsTruePayRegTabClick()
        {
            
            DGBY.Model.NotLogisticsTruePayRegTab modelNotLogisticsTruePayRegTab = m_bllNotLogisticsTruePayRegTab.GetModel(m_nSelectNotLogisticsTruePayRegTabID);

            if (modelNotLogisticsTruePayRegTab != null)
            {
                this.textBoxWhetherGet.Text = modelNotLogisticsTruePayRegTab.WhetherGet;
                this.textBoxBianHao.Text = modelNotLogisticsTruePayRegTab.BianHao;
                this.textBoxMonth.Text = modelNotLogisticsTruePayRegTab.PayMonth.ToString();
                this.textBoxContractNo.Text = modelNotLogisticsTruePayRegTab.ContractNo;
                this.textBoxTruePay.Text = modelNotLogisticsTruePayRegTab.TruePayMoney.ToString();
                this.textBoxTruePayItem.Text = modelNotLogisticsTruePayRegTab.TruePayItem;

                this.textBoxAgentPerson.Text = modelNotLogisticsTruePayRegTab.AgentPerson;
                this.comboBoxProvider.Text = modelNotLogisticsTruePayRegTab.Provider;

                this.textBoxPublicFee.Text = modelNotLogisticsTruePayRegTab.PublicFee;
                this.textBoxPrivateFee.Text = modelNotLogisticsTruePayRegTab.PrivateFee;
                this.textBoxForeignJinE.Text = modelNotLogisticsTruePayRegTab.ForeignMoneyJinE.ToString();

                this.dateTimePickerMoneyComeDate.Value = (System.DateTime)modelNotLogisticsTruePayRegTab.HuiKuanDate;

                if (modelNotLogisticsTruePayRegTab.FactoryDataTabID != null)
                    this.comboBoxFac.SelectedValue = modelNotLogisticsTruePayRegTab.FactoryDataTabID;
                if (modelNotLogisticsTruePayRegTab.BiZhiTabID != null)
                    this.comboBoxMoneyType.SelectedValue = modelNotLogisticsTruePayRegTab.BiZhiTabID;
                if (modelNotLogisticsTruePayRegTab.BYDepartTabID != null)
                    this.comboBoxDep.SelectedValue = modelNotLogisticsTruePayRegTab.BYDepartTabID;
                if (modelNotLogisticsTruePayRegTab.BYCompanyTabID != null)
                    this.comboBoxComp.SelectedValue = modelNotLogisticsTruePayRegTab.BYCompanyTabID;
                
            }
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.NotLogisticsTruePayRegTab modelNotLogisticsTruePayRegTab = m_bllNotLogisticsTruePayRegTab.GetModel(m_nSelectNotLogisticsTruePayRegTabID);

                if (modelNotLogisticsTruePayRegTab == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxTruePay))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxForeignJinE))
                    return;
                if (ClassInputValid.IsNum(this.errorProvider1, this.textBoxMonth))
                    return;

                modelNotLogisticsTruePayRegTab.WhetherGet = this.textBoxWhetherGet.Text;


                //modelNotLogisticsTruePayRegTab.BianHao = MakeBianHao(this.comboBoxDep.Text);
                modelNotLogisticsTruePayRegTab.HuiKuanDate = this.dateTimePickerMoneyComeDate.Value;
                modelNotLogisticsTruePayRegTab.PayMonth = Convert.ToInt32(this.textBoxMonth.Text);
                modelNotLogisticsTruePayRegTab.ContractNo = this.textBoxContractNo.Text;
                modelNotLogisticsTruePayRegTab.TruePayMoney = Convert.ToDecimal(this.textBoxTruePay.Text);

                modelNotLogisticsTruePayRegTab.TruePayItem = this.textBoxTruePayItem.Text;
                modelNotLogisticsTruePayRegTab.AgentPerson = this.textBoxAgentPerson.Text;
                modelNotLogisticsTruePayRegTab.Provider = this.comboBoxProvider.Text;
                modelNotLogisticsTruePayRegTab.PublicFee = this.textBoxPublicFee.Text;
                modelNotLogisticsTruePayRegTab.PrivateFee = this.textBoxPrivateFee.Text;
                modelNotLogisticsTruePayRegTab.ForeignMoneyJinE = Convert.ToDecimal(this.textBoxForeignJinE.Text);

                if (this.comboBoxFac.SelectedValue != null)
                    modelNotLogisticsTruePayRegTab.FactoryDataTabID = Convert.ToInt32(this.comboBoxFac.SelectedValue.ToString(), 10);

                if (this.comboBoxMoneyType.SelectedValue != null)
                    modelNotLogisticsTruePayRegTab.BiZhiTabID = Convert.ToInt32(this.comboBoxMoneyType.SelectedValue.ToString(), 10);

                if (this.comboBoxDep.SelectedValue != null)
                    modelNotLogisticsTruePayRegTab.BYDepartTabID = Convert.ToInt32(this.comboBoxDep.SelectedValue.ToString(), 10);

                if (this.comboBoxComp.SelectedValue != null)
                    modelNotLogisticsTruePayRegTab.BYCompanyTabID = Convert.ToInt32(this.comboBoxComp.SelectedValue.ToString(), 10);




                m_bllNotLogisticsTruePayRegTab.Update(modelNotLogisticsTruePayRegTab);

                MessageBox.Show("数据修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }
        private void ResetAllTextBox(Form form)
        {
            for (int i = 0; i < form.Controls.Count; i++)
            {
                if (form.Controls[i] is TextBox)
                {
                    (form.Controls[i] as TextBox).Text = "";
                }

                if (form.Controls[i] is GroupBox)
                {
                    for (int j = 0; j < form.Controls[i].Controls.Count; j++)
                    {
                        if (form.Controls[i].Controls[j] is TextBox)
                        {
                            (form.Controls[i].Controls[j] as TextBox).Text = "";
                        }
                    }
                }
            }
        }
        private void buttonDel_Click(object sender, EventArgs e)
        {
            if (m_bllNotLogisticsTruePayRegTab.GetModel(m_nSelectNotLogisticsTruePayRegTabID) == null)
            {
                MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                m_bllNotLogisticsTruePayRegTab.Delete(m_nSelectNotLogisticsTruePayRegTabID);

                ShowAllRecordInDataGridView("");
                MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ResetAllTextBox(this);
                return;
            }
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            this.ultraGridPrintDocument1.Grid = this.ultraGrid1;
            this.ultraPrintPreviewDialog1.Document = this.ultraGridPrintDocument1;
            this.ultraPrintPreviewDialog1.ShowDialog(this);
        }
       
        private void InitQueryCondition()
        {
            this.comboBoxLogic.SelectedIndex = 0;
            this.comboBoxQueryCondition.SelectedIndex = 0;
            this.radioButtonValueIs.Select();
            dicQueryCondition.Add("是否已提取", "WhetherGet");
            dicQueryCondition.Add("编号", "BianHao");
            dicQueryCondition.Add("汇款日期", "HuiKuanDate");
            dicQueryCondition.Add("实付项目所属月", "PayMonth");
            dicQueryCondition.Add("工厂名称", "FactoryDataTab.FactoryName");
            dicQueryCondition.Add("合同编号", "ContractNo");
            dicQueryCondition.Add("实付金额", "TruePayMoney");
            dicQueryCondition.Add("实付项目", "TruePayItem");
            dicQueryCondition.Add("经办人", "AgentPerson");
            dicQueryCondition.Add("经营单位", "BYCompanyTab.CompanyName");
            dicQueryCondition.Add("供应商", "Provider");
            dicQueryCondition.Add("对公汇款", "PublicJinE");
            dicQueryCondition.Add("对私汇款", "PrivateJinE");
            dicQueryCondition.Add("外币金额", "ForeignMoneyJinE");
            dicQueryCondition.Add("币种", "BiZhiTab.BiZhiName");

        }

        private bool IsNum(string strInput)
        {
            return Regex.IsMatch(strInput, @"^[1-9]+\d*$");
        }

        private bool ValidDate(string strInput)
        {
            if (strInput.Length != 8)
                return false;
            else
                return IsNum(strInput);
        }

        private void StartQuery(bool bIsQueryInRes)
        {
            string strWhere = "";

            string strTemp = this.comboBoxQueryCondition.SelectedItem.ToString();
            if (this.radioButtonBetween.Checked)
            {

                if (strTemp == "汇款日期")
                {
                    if (strTemp == "汇款日期")
                    {
                        if (!ValidDate(this.textBoxFrom.Text) || !ValidDate(this.textBoxTo.Text))
                        {
                            MessageBox.Show("日期格式有误，正确的格式如 20100808 !", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }

                        if (this.textBoxFrom.Text != "" && this.textBoxTo.Text != "")
                        {
                            strWhere = "CONVERT(varchar(8), " + dicQueryCondition[strTemp] + ", 112) >= " + "'" + this.textBoxFrom.Text + "'" + " and CONVERT(varchar(8), " + dicQueryCondition[strTemp] + ", 112) <= " + "'" + this.textBoxTo.Text + "'";
                        }
                        else
                        {
                            MessageBox.Show("输入的查询条件有错误，请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }
                    else
                    {
                        if (this.textBoxFrom.Text != "" && this.textBoxTo.Text != "")
                        {
                            strWhere = dicQueryCondition[strTemp] + " BETWEEN " + "'" + this.textBoxFrom.Text + "'" + " AND " + "'" + this.textBoxTo.Text + "'";
                        }
                        else
                        {
                            MessageBox.Show("输入的查询条件有错误，请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("输入的查询条件有错误，请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                if (strTemp == "汇款日期")
                {
                    if (this.textBoxValueIs.Text != "")
                    {
                        if (!ValidDate(this.textBoxValueIs.Text))
                        {
                            MessageBox.Show("日期格式有误，正确的格式如 20100808 !", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                        strWhere = this.textBoxValueIs.Text + " LIKE CONVERT(varchar(8), " + dicQueryCondition[strTemp] + ", 112) ";
                    }
                    else
                    {
                        MessageBox.Show("输入的查询条件有错误，请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
                else
                {
                    strWhere = dicQueryCondition[strTemp] + " LIKE " + "'%" + this.textBoxValueIs.Text + "%'";
                }
            }

            if (bIsQueryInRes == false)
            {
                m_strQueryWhere = strWhere;
            }
            else
            {
                if (m_strQueryWhere != "")
                {
                    if (this.comboBoxLogic.SelectedIndex == 0)
                    {
                        m_strQueryWhere = "(" + m_strQueryWhere + " AND " + strWhere + ")";
                    }
                    else if (this.comboBoxLogic.SelectedIndex == 1)
                    {
                        m_strQueryWhere = "(" + m_strQueryWhere + " OR " + strWhere + ")";
                    }
                    else
                    {
                        MessageBox.Show("查询条件有误，请选择\"并且\"或\"或者\"!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                }
                else
                {
                    MessageBox.Show("没有所基于的查询结果，请首先单据\"查询\"按键!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

            }

            ShowAllRecordInDataGridView(m_strQueryWhere);
        }

        private void buttonQuery_Click(object sender, EventArgs e)
        {
            StartQuery(false);
        }

        private void buttonQueryFromResult_Click(object sender, EventArgs e)
        {
            StartQuery(true);
        }

        private void radioButtonValueIs_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButtonValueIs.Checked)
            {
                this.textBoxFrom.Text = "";
                this.textBoxTo.Text = "";
            }
        }

        private void radioButtonBetween_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButtonBetween.Checked)
                this.textBoxValueIs.Text = "";
        }

        private void ultraGridExcelExporter1_CellExported(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.CellExportedEventArgs e)
        {

        }
    }
}
